<?php
namespace Home\Controller;
use Think\Controller;
class ProductController extends Controller {
    public function detail($id){
    	$product = M('product')->where('id  ='.$id)->find();
    	$imgs = M('product_img')->where('product_id = '.$id)->limit(5)->select();
    	$this->assign('product',$product);
    	$this->assign('imgs',$imgs);
    	$this->display();
    }
    public function lists($cataid){
        $brand_id = I('get.brand');
    	$current_catalog = M('product_catalog')->where('id = '.$cataid)->find();//获取当前分类内容
    	$parent_id = $current_catalog['parent_id'];
    	$parent_catalog = M('product_catalog')->where('id = '.$parent_id)->find();//获取当前分类的父分类的内容
     	if ($current_catalog['parent_id']== 0) {
     		$brother_catalog = M('product_catalog')->where('parent_id = '.$cataid)->select();
    		$this->redirect('home/product/lists/cataid/'.$brother_catalog[0]['id']);
		 }
    	$brother_catalogs = M('product_catalog')->where('parent_id = '.$parent_id)->select();//获取兄弟分类的内容
    	$children_catalog = M('product_catalog')->where('parent_id = '.$cataid)->select();
    	$catalogs = array($cataid);
    	foreach ($children_catalog as $c) {
    		$catalogs[] = $c['id'];
    	}
        $where = 'cata_id in ('.implode(',', $catalogs).')';
        if($brand_id){
            $where .= ' and brand_id='.$brand_id; 
        }
    	$products = M('product')->where($where)->select();
        $temp = M('product')->distinct(true)->where('cata_id in ('.implode(',', $catalogs).')')->field('brand_id')->select();
    	$brand_ids = array();
        foreach ($temp as $t) {
            $brand_ids[] = $t['brand_id'];
        }
    	$brands = M('product_brand')->where('id in ('.implode(',', $brand_ids).')')->select();

    	$this->assign('catalog',$current_catalog);
    	$this->assign('parent_catalog',$parent_catalog);
    	$this->assign('brother_catalogs',$brother_catalogs);
    	$this->assign('products',$products);
    	$this->assign('brands',$brands);
        $this->assign('brand_id',$brand_id);
    	$this->display();
    }		
}